package com.ebelter.seneo.ui.activitys;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebelter.btcomlib.models.bluetooth.base.BlueManager;
import com.ebelter.btcomlib.models.bluetooth.interfaces.IBlueStationListener;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.contants.IUserContant;
import com.ebelter.seneo.ui.fragments.addinfos.AddInfo_Athlete_Fragment;
import com.ebelter.seneo.ui.fragments.addinfos.AddInfo_Birthday_Fragment;
import com.ebelter.seneo.ui.fragments.addinfos.AddInfo_Height_Fragment;
import com.ebelter.seneo.ui.fragments.addinfos.AddInfo_Sex_Fragment;
import com.ebelter.seneo.ui.fragments.bonddevices.BondDeviceSuccessFragment;
import com.ebelter.seneo.ui.fragments.bonddevices.BondOpenBTTipFragment;
import com.ebelter.seneo.ui.fragments.bonddevices.BondScanDevicesFragment;
import com.ebelter.seneo.ui.fragments.bonddevices.BondScanFailTipFragment;

public class SeneoBondDeviceActivity extends SeneoBaseActivity implements View.OnClickListener {



    ImageView top_iv;
    TextView top_tv1;

    TextView modify_psw_next_bt;


    private static final int F_OPENTIP=0;
    private static final int F_SCANING=1;
    public static final int F_SCAN_FAILURE=2;
    private static final int F_BONE_SUCCESS=3;
    private int currentDisplayFragment;

    BondOpenBTTipFragment mBondOpenBTTipFragment;
    BondScanDevicesFragment mBondScanDevicesFragment;
    BondScanFailTipFragment mBondScanFailTipFragment;
    BondDeviceSuccessFragment mBondDeviceSuccessFragment;




    @Override
    protected int loadLayoutById() {
        return R.layout.activity_common1_ly;
    }
    @Override
    protected void initView() {
        // StatusBarUtil.setColor(this, Color.parseColor("#ff0000"));


        top_iv=findViewById(R.id.top_iv);
        top_iv.setImageResource(R.drawable.ic_back);
        top_tv1=findViewById(R.id.top_tv1);
        ViewUtils.setTextViewStr(top_tv1,"");
        ViewUtils.displayView(top_tv1);
        modify_psw_next_bt=findViewById(R.id.modify_psw_next_bt);

        ViewUtils.displayView(top_iv);
        top_iv.setOnClickListener(this);
        modify_psw_next_bt.setOnClickListener(this);

        ViewUtils.setEnabled(modify_psw_next_bt,false);
        if (BlueManager.getInstance().isBluetoothOpen()){
            displayFragment(F_SCANING);
        }else {
            displayFragment(F_OPENTIP);
        }
        BlueManager.getInstance().addBluetoothStationListener(mIBlueStationListener);
    }


    public void setNextBt(boolean enable){
        ViewUtils.setEnabled(modify_psw_next_bt,enable);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BlueManager.getInstance().removeBluetoothStationListener(mIBlueStationListener);
    }

    private final IBlueStationListener mIBlueStationListener=new IBlueStationListener() {
        @Override
        public void STATE_OFF() {
            if (isCouldOperationFlag){
                displayFragment(F_OPENTIP);//检测蓝牙开关 只要关闭蓝牙就显示打开蓝牙界面
                ViewUtils.setEnabled(modify_psw_next_bt,false);
            }

        }

        @Override
        public void STATE_TURNING_OFF() {}

        @Override
        public void STATE_ON() {
            if (isCouldOperationFlag){
                displayFragment(F_SCANING);
            }
        }

        @Override
        public void STATE_TURNING_ON() {}
    };

     boolean isCouldOperationFlag;

    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
        isCouldOperationFlag=true;
    }


    @Override
    protected void onPause() {
        super.onPause();
        isCouldOperationFlag=false;
    }

    public void displayFragment(int whichFragment) {
        currentDisplayFragment=whichFragment;
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideAllFragment(transaction, mBondOpenBTTipFragment,mBondScanDevicesFragment,mBondScanFailTipFragment,mBondDeviceSuccessFragment);//隐藏全部的fragment

        int contain = R.id.modifypsw_container;//容器
        ViewUtils.setTextViewStr(modify_psw_next_bt,R.string.next);//设置下一步
        if (whichFragment==F_OPENTIP){
            if (mBondOpenBTTipFragment == null) {
                mBondOpenBTTipFragment = new BondOpenBTTipFragment();
                transaction.add(contain, mBondOpenBTTipFragment);
            } else {
                transaction.show(mBondOpenBTTipFragment);
            }

            setTopStr(StringUtils.getResStr(R.string.turn_on_bluetooth));//打开蓝牙

        }else if (whichFragment==F_SCANING){

            if (mBondScanDevicesFragment == null) {
                mBondScanDevicesFragment = new BondScanDevicesFragment();
                transaction.add(contain, mBondScanDevicesFragment);
                mBondScanDevicesFragment.initDataFile(this);
            } else {
                transaction.show(mBondScanDevicesFragment);
            }

            ViewUtils.setEnabled(modify_psw_next_bt,false);
            setTopStr("");

            if (mBondScanDevicesFragment.getmBondAdapter()!=null){
                mBondScanDevicesFragment.getmBondAdapter().checkAddress = null;
                mBondScanDevicesFragment.getmBondAdapter().checkName = null;
            }
            mBondScanDevicesFragment.startScan();

        } else if (whichFragment==F_SCAN_FAILURE){

            if (mBondScanFailTipFragment == null) {
                mBondScanFailTipFragment = new BondScanFailTipFragment();
                transaction.add(contain, mBondScanFailTipFragment);
            } else {
                transaction.show(mBondScanFailTipFragment);
            }
            ViewUtils.setTextViewStr(modify_psw_next_bt,StringUtils.getResStr(R.string.Search_again));//重新搜索
            setTopStr(StringUtils.getResStr(R.string.Search_failed));//搜索失败
            ViewUtils.setEnabled(modify_psw_next_bt,true);

        }else if (whichFragment==F_BONE_SUCCESS){

            if (mBondDeviceSuccessFragment == null) {
                mBondDeviceSuccessFragment = new BondDeviceSuccessFragment();
                transaction.add(contain, mBondDeviceSuccessFragment);
            } else {
                transaction.show(mBondDeviceSuccessFragment);
            }

            mBondDeviceSuccessFragment.setNameAddress(mBondScanDevicesFragment.getCheckBluetooth());
            ViewUtils.setTextViewStr(modify_psw_next_bt,"Pairing");
            setTopStr("Pair device");//关联设备
        }
        currentDisplayFragment=whichFragment;
        transaction.commit();




    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.top_iv:
                back();
                break;
            case R.id.modify_psw_next_bt:
                next();
                break;
        }
    }

    private void next() {

        if (currentDisplayFragment==F_OPENTIP){
            displayFragment(F_SCANING);
        }else if (currentDisplayFragment==F_SCANING){
            displayFragment(F_BONE_SUCCESS);
        }else if (currentDisplayFragment==F_SCAN_FAILURE){
            displayFragment(F_SCANING);
        }else if (currentDisplayFragment==F_BONE_SUCCESS){
            done();
        }

    }

    //完成按钮
    private void done() {
        String[] nameAdd = mBondDeviceSuccessFragment.getNameAddress();
        if (nameAdd==null){
            ToastUtil.show("error 1001");
        }else {
            UserSpUtil.writeString(IUserContant.IUser.bondAddress,nameAdd[0]);
            UserSpUtil.writeString(IUserContant.IUser.bondName,nameAdd[1]);
            ToastUtil.show("Paired");
            UserSpUtil.writeBoolean("getBluetoothGattConnectSuccessFlag",true);//每次重新绑定的时候还是把这个标志置为可以连接成功
            finish();



        }


    }

    private void back() {
        if (currentDisplayFragment==F_OPENTIP||currentDisplayFragment==F_SCANING){
            finish();
        }else {
            currentDisplayFragment=F_SCANING;
            displayFragment(F_SCANING);
        }
    }

    @Override
    public void onBackPressed() {
        back();
    }


    public void setTopStr(String topStr){
        ViewUtils.setTextViewStr(top_tv1,topStr);
    }
}
